package com.task.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.task.dao.ExperienceDao;
import com.task.dao.TaskDao;
import com.task.entity.Experience;
import com.task.entity.Task;
import com.task.entity.User;

public class TaskAction extends ActionSupport {

	private TaskDao taskDao;
	
	private Task task;
	
	HttpServletResponse response=null;
	
	private String date;
	
	Map session=null;
	
	private ExperienceDao experienceDao;

	public void setExperienceDao(ExperienceDao experienceDao) {
		this.experienceDao = experienceDao;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}
	
	//更改任务状态
	public String updateTaskState() throws Exception{
		taskDao.updateTaskStateByTaskId(task.getTaskId(), task.getTaskState());
	    response=ServletActionContext.getResponse();
		response.getWriter().write("1");
		return null;
	}
	//更改任务为收纳箱里面的nav
	public String doAjaxUpdateToMenuNav() throws Exception{
		 taskDao.updateNavToMenuNav(task.getTaskId(), task.getTask().getTaskId());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	//更改任务为四象限里面的nav
	public String doAjaxUpdateNavToOnelistNav() throws Exception{
		 taskDao.updateNavToOnelistNav(task.getTaskId(), date, task.getTaskType());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	//更改任务为收纳箱里面的menutitle
	public String doAjaxUpdateNavToMenuTitle() throws Exception{
		 taskDao.updateNavToMenuTitle(task.getTaskId());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	//更改任务为四象限的nav
	public String doAjaxUpdateMenuTitleToOnelistNav() throws Exception{
		 taskDao.updateMenuTitleToOnelistNav(task.getTaskId(),date,task.getTaskType());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	//更改任务为收纳箱中的nav
	public String doAjaxUpdateMenuTitleToNav() throws Exception{
		 taskDao.updateMenuTitleToNav(task.getTaskId(),task.getTask().getTaskId());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	
	//添加任务
	public String doAddTask() throws Exception{
		 session=ActionContext.getContext().getSession();
		 User user1=(User) session.get("LOGINED_USER");
		 task.setTaskDate(null);
		 task.setTask(null);
		 task.setTaskState(0);
		 task.setTaskType(0);
		 task.setTaskUser(user1);
		 int taskId=taskDao.addTask(task);
		 response=ServletActionContext.getResponse();
		 response.getWriter().write(taskId+"");
		 System.out.println(taskId+"-------");
		 return null;
	}
	//根据选中日期查询任务
	public String getTeakByBigDate() throws Exception{
		 session=ActionContext.getContext().getSession();
		 response=ServletActionContext.getResponse();
		 response.setContentType("text/html;charset=UTF-8");
		 Integer userId = ((User) session.get("LOGINED_USER")).getUserId();
			List<Task> tasks1 = taskDao.getTaskByDateAndType(userId,date,1,0);
			List<Task> tasks2 = taskDao.getTaskByDateAndType(userId,date,2,0);
			List<Task> tasks3 = taskDao.getTaskByDateAndType(userId,date,3,0);
			List<Task> tasks4 = taskDao.getTaskByDateAndType(userId,date,4,0);
			
			StringBuffer buffer=new StringBuffer("[");
			for(int i=0;i<tasks1.size();i++){
				Task task=tasks1.get(i);				 
				buffer.append("{type:\""+1+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks2.size();i++){
				Task task=tasks2.get(i);				 
				buffer.append("{type:\""+2+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks3.size();i++){
				Task task=tasks3.get(i);				 
				buffer.append("{type:\""+3+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks4.size();i++){
				Task task=tasks4.get(i);				 
				buffer.append("{type:\""+4+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
					
			String result=buffer.toString().substring(0, buffer.toString().length()-1)+"]";
			System.out.println(result);
			response.getWriter().write(result);
			return null;
	}
	
	public String getTeakBySmallDate() throws Exception{
		 session=ActionContext.getContext().getSession();
		 response=ServletActionContext.getResponse();
		 response.setContentType("text/html;charset=UTF-8");
		 Integer userId = ((User) session.get("LOGINED_USER")).getUserId();
			List<Task> tasks1 = taskDao.getTaskByDateAndType(userId,date,1,1); 
			List<Task> tasks2 = taskDao.getTaskByDateAndType(userId,date,2,1);
			List<Task> tasks3 = taskDao.getTaskByDateAndType(userId,date,3,1);
			List<Task> tasks4 = taskDao.getTaskByDateAndType(userId,date,4,1);			
			StringBuffer buffer=new StringBuffer("[");
			for(int i=0;i<tasks1.size();i++){
				Task task=tasks1.get(i);				 
				buffer.append("{type:\""+1+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks2.size();i++){
				Task task=tasks2.get(i);				 
				buffer.append("{type:\""+2+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks3.size();i++){
				Task task=tasks3.get(i);				 
				buffer.append("{type:\""+3+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			for(int i=0;i<tasks4.size();i++){
				Task task=tasks4.get(i);				 
				buffer.append("{type:\""+4+"\",id:\""+task.getTaskId()+"\",name:\""+task.getTaskName()+"\"},");										
			}
			String result=buffer.toString().substring(0, buffer.toString().length()-1)+"]";
			System.out.println(result);
			response.getWriter().write(result);
			return null;
	}
	 
	//删除
	public String doDelTaskById() throws Exception{
		 taskDao.delTaskByParentId(task.getTaskId());
		 taskDao.delTaskById(task.getTaskId());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;	
	}
	//根据编号获取实体类
	public String getTaskById() throws Exception{
		 Task task1=taskDao.getTaskById(task.getTaskId());
		 response=ServletActionContext.getResponse();
		 response.setContentType("text/html;charset=UTF-8");
		 response.getWriter().write(task1.getTaskName());
		 return null;
	}
	//更改任务名称
	public String updateTaskName() throws Exception{
		 taskDao.updateTaskName(task.getTaskId(), task.getTaskName());
		 response=ServletActionContext.getResponse();
		 response.getWriter().write("1");
		 return null;
	}
	
	//ajax
	public String getData() throws Exception{
		List<Task> preTask=taskDao.getPreTask();
		Set<String> strs=new HashSet<String>();
		for(int i=0;i<preTask.size();i++)
		{
			String tempStr=preTask.get(i).getTaskDate().toString().substring(0, 10);
			String one=tempStr.substring(0, 4);
			String two=tempStr.substring(5, 7);
			String three=tempStr.substring(8, 10);
			strs.add(one+two+three);
		}
		Iterator<String> it = strs.iterator();
		String outputStr="";
		while(it.hasNext()){
			outputStr+=it.next()+";";
		}
		response=ServletActionContext.getResponse();
		response.getWriter().write(outputStr.substring(0,outputStr.length()-1));
		System.out.println(outputStr.substring(0,outputStr.length()-1));
		return null;		
	}
}
